package kr.hkit.summary_java.erp;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.db.DbUtil;
import kr.hkit.summary_java.dao.CodeTitleDAO;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.common.MenuType;
import kr.hkit.summary_java.erp.dto.CodeTitle;
import kr.hkit.summary_java.erp.dto.Department;
import kr.hkit.summary_java.erp.dto.Employee;
import kr.hkit.summary_java.serviceUI.PanelDepartmentUI;
import kr.hkit.summary_java.serviceUI.PanelEmpUI;
import kr.hkit.summary_java.serviceUI.PanelListUI;
import kr.hkit.summary_java.serviceUI.PanelTitleUI;

public class Main extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private String[] menuTitle;
	
	private DepartmentDAO deptDao;
	private EmployeeDAO empDao;
	private CodeTitleDAO titleDao;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		});
	}

	public Main() {
		setTitle("SummaryJava_ERP Project");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		ErpMenu jMenu = new ErpMenu("부서");
		menuBar.add(jMenu);
		
		ErpMenu jMenu1 = new ErpMenu("사원");
		menuBar.add(jMenu1);
		
		ErpMenu jMenu2 = new ErpMenu("직책");
		menuBar.add(jMenu2);
		
		setJMenuBar(menuBar);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		addAction();
		
		deptDao = new DepartmentDAO();
		empDao = new EmployeeDAO();
		titleDao = new CodeTitleDAO();
		empDao = new EmployeeDAO();
		
	}
	
	public void addAction(){
		int menuCnt = menuBar.getMenuCount();
		menuTitle = new String[menuCnt];
		
		for(int i=0; i<menuCnt;i++){
			menuTitle[i] = menuBar.getMenu(i).getText();
		}

		for(int j=0; j<menuCnt; j++){
			for(int i=0; i<menuBar.getMenu(j).getItemCount();i++){
				menuBar.getMenu(j).getItem(i).addActionListener(this);
			}
		}
		
	}
	
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()){
			case "부서 추가":actionDeptAdd(e);break;
			case "부서 검색":actionDeptSearch(e);break;
			case "부서 수정":actionDeptUpdate(e);break;
			case "부서 삭제":actionDeptDel(e);break;
			case "부서 리스트":actionDeptList(e);break;
			
			case "사원 추가":actionEmpAdd(e);break;
			case "사원 검색":actionEmpSearch(e);break;
			case "사원 수정":actionEmpUpdate(e);break;
			case "사원 삭제":actionEmpDel(e);break;
			case "사원 리스트":actionEmpList(e);break;
			
			case "직책 추가":actionTitleAdd(e);break;
			case "직책 검색":actionTitleSearch(e);break;
			case "직책 수정":actionTitleUpdate(e);break;
			case "직책 삭제":actionTitleDel(e);break;
			case "직책 리스트":actionTitleList(e);break;
		}
	}

	private void revalidPanel(JPanel panel){
		contentPane.removeAll();
		contentPane.add(panel);
		pack();
		revalidate();
	}
	
	private void revalidPanel(PanelListUI<?> panel){
		contentPane.removeAll();
		contentPane.add(panel);
		pack();
		revalidate();
	}
	
	protected void actionDeptUpdate(ActionEvent e) {
		revalidPanel(new PanelDepartmentUI("수정",deptDao));
	}
	
	protected void actionDeptAdd(ActionEvent e) {
		revalidPanel(new PanelDepartmentUI("추가",deptDao));
	}
	 
	private void actionDeptSearch(ActionEvent e) {
		revalidPanel(new PanelDepartmentUI("검색",deptDao));
		
	}
	protected void actionDeptDel(ActionEvent e) {
		revalidPanel(new PanelDepartmentUI("삭제",deptDao));
	}
	
	protected void actionDeptList(ActionEvent e) {
		revalidPanel(new PanelListUI<Department>(MenuType.DEPARTMENT, deptDao));
	}
	
	protected void actionEmpUpdate(ActionEvent e) {
		revalidPanel(new PanelEmpUI("수정",empDao));
	}
	
	protected void actionEmpAdd(ActionEvent e) {
		revalidPanel(new PanelEmpUI("추가",empDao));
	}
	 
	private void actionEmpSearch(ActionEvent e) {
		revalidPanel(new PanelEmpUI("검색",empDao));
		
	}
	protected void actionEmpDel(ActionEvent e) {
		revalidPanel(new PanelEmpUI("삭제",empDao));
	}
	
	protected void actionEmpList(ActionEvent e) {
		revalidPanel(new PanelListUI<Employee>(MenuType.EMPLOYEE, empDao));
	}
	
	protected void actionTitleUpdate(ActionEvent e) {
		revalidPanel(new PanelTitleUI("수정",titleDao));
	}
	
	protected void actionTitleAdd(ActionEvent e) {
		revalidPanel(new PanelTitleUI("추가",titleDao));
	}
	 
	private void actionTitleSearch(ActionEvent e) {
		revalidPanel(new PanelTitleUI("검색",titleDao));
		
	}
	protected void actionTitleDel(ActionEvent e) {
		revalidPanel(new PanelTitleUI("삭제",titleDao));
	}
	
	protected void actionTitleList(ActionEvent e) {
		revalidPanel(new PanelListUI<CodeTitle>(MenuType.TITLE, titleDao));
	}
}
